<?php
/**
 * function_exists() 函数用于检测函数是否被定义。
 */
// 获取当前登录用户
if (! function_exists('auth_user')) {
    /**
     * Get the auth_user.
     *
     * @return mixed
     */
    function auth_user()
    {
        return app('Dingo\Api\Auth\Auth')->user();
    }
}

if (! function_exists('dingo_route')) {
    /**
     * 根据别名获得url.
     *
     * @param string $version
     * @param string $name
     * @param string $params
     *
     * @return string
     */
    function dingo_route($version, $name, $params = [])
    {
        return app('Dingo\Api\Routing\UrlGenerator')
            ->version($version)
            ->route($name, $params);
    }
}

if (! function_exists('trans')) {
    /**
     * Translate the given message.
     *
     * @param string $id
     * @param array  $parameters
     * @param string $domain
     * @param string $locale
     *
     * @return string
     */
    function trans($id = null, $parameters = [], $domain = 'messages', $locale = null)
    {
        if (is_null($id)) {
            return app('translator');
        }

        return app('translator')->trans($id, $parameters, $domain, $locale);
    }
}

//截取指定位置指定长度的字符串
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=false){

    if(function_exists("mb_substr")){

        if($suffix)

            return mb_substr($str, $start, $length, $charset)."...";

        else

            return mb_substr($str, $start, $length, $charset);

    }elseif(function_exists('iconv_substr')) {

        if($suffix)

            return iconv_substr($str,$start,$length,$charset)."...";

        else

            return iconv_substr($str,$start,$length,$charset);

    }

    $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";

    $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";

    $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";

    $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";

    preg_match_all($re[$charset], $str, $match);

    $slice = join("",array_slice($match[0], $start, $length));

    if($suffix) return $slice."…";

    return $slice;

}
if (! function_exists('bcrypt')) {
    /**
     * Hash the given value.
     *
     * @param  string  $value
     * @param  array   $options
     * @return string
     */
    function bcrypt($value, $options = [])
    {
        return app('hash')->make($value, $options);
    }
}

/**
 * 根据课程编码 返回培训项目名称
 * @param $subjcode
 * @return mixed
 */
function getPutimeName($subjcode)
{
    $key = substr($subjcode,4,2);
    $data = [
        '00'=>"基础驾驶培训",
        '01'=>"法律、法规及道路交通信号",
        "02"=>"机动车基本知识",
        "03"=>"第一部分综合复习及考核",
        "11"=>"基础驾驶",
        "12"=>"场地驾驶",
        "13"=>"第二部分综合驾驶及考核",
        "21"=>"跟车行驶",
        "22"=>"变更车道",
        "23"=>"靠边停车",
        "24"=>"掉头",
        "25"=>"通过路口",
        "26"=>"通过人行横道",
        "27"=>"通过学校区域",
        "28"=>"通过公共汽车站",
        "29"=>"会车",
        "30"=>"超车",
        "31"=>"夜间驾驶",
        "32"=>"恶劣条件下的驾驶",
        "33"=>"山区道路驾驶",
        "34"=>"高速公路驾驶",
        "35"=>"行驶路线选择",
        "36"=>"第三部分综合驾驶及考核",
        "41"=>"安全、文明驾驶知识",
        "42"=>"危险源辨识知识",
        "43"=>"夜间和高速公路安全驾驶知识",
        "44"=>"恶劣气象和复杂道路条件下的安全驾驶知识",
        "45"=>"紧急情况应急处置知识",
        "46"=>"危险化学品知识",
        "47"=>"典型事故案例分析",
        "48"=>"第四部分综合复",
    ];
    return $data[$key];
}

/**
 * @param $url
 * @return mixed
 */
function get_curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
/**
 * 转换地图坐标
 * @param $point
 * @return float
 */
function changepoint($point){
    return $point/1000000;
}

/**gps转百度坐标
 * @param $x
 * @param $y
 * @param int $type
 * @return bool
 */
function gps_convert($x,$y,$type=4){
    $url = "http://api.map.baidu.com/ag/coord/convert?from=0&to=".$type."&x=".$x."&y=".$y;
    $result = get_curl($url);
    $point_res = json_decode($result,true);
    if($point_res['error'] == 0){
        $point[0] = base64_decode($point_res['x']);
        $point[1] = base64_decode($point_res['y']);
        return $point;
    }else{
        return false;
    }
}



function traintypeName($traintype)
{
    switch ($traintype){
        case 'A1':
            return 'A1大型客车';
            breack;
        case 'A2':
            return 'A2牵引车';
            breack;
        case 'A3':
            return '城市公交车';
            breack;
        case 'B1':
            return 'B1中型客车';
            breack;
        case 'B2':
            return 'B2大型货车';
            breack;
        case 'C1':
            return 'C1小型汽车';
            breack;
        case 'C2':
            return 'C2小型自动挡汽车';
            breack;
        case 'C3':
            return 'C3大型客车';
            breack;
        case 'C4':
            return 'C4三轮汽车';
            breack;
        case 'D':
            return 'D普通三轮摩托车';
            breack;
        case 'E':
            return 'E普通二轮摩托车';
            breack;
        case 'F':
            return 'F轻便摩托车';
            breack;
        case 'M':
            return 'M轮式自行机械车';
            breack;
        case 'N':
            return 'N无轨电车';
            breack;
        case 'P':
            return 'P有轨电车';
            breack;
    }
}
function curlToJson($url,$data)
{
    $headers = array("Content-type: application/json;charset='utf-8'", "Accept: application/json", "Cache-Control: no-cache","Pragma: no-cache",);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60); //设置超时
    if(0 === strpos(strtolower($url), 'https')) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //从证书中检查SSL加密算法是否存在
    }
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $rtn = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置  curl_exec返回TRUE 设置  curl_exec返回json(此处) 失败都返回FALSE
    curl_close($ch);
    return $rtn;
}
/**
 * 去除字符串首尾空格
 * @param $value
 * @return string
 */
function removeSpaces($value)
{
    $value = mb_ereg_replace('^(　| )+', '', $value);
    $value = mb_ereg_replace('(　| )+$', '', $value);
    return mb_ereg_replace('　　', "\n　　", $value);
}
function getfirstchar($s0){
    if(empty($s0) || $s0==' ') return false;
    $fchar = ord($s0{0});
    if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
    try{
        $s1 = iconv("UTF-8","gb2312", $s0);
        $s2 = iconv("gb2312","UTF-8", $s1);
    }catch (\Exception $e)
    {
        return false;
    }
    if($s2 == $s0){$s = $s1;}else{$s = $s0;}
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
    if($asc >= -20319 and $asc <= -20284) return "A";
    if($asc >= -20283 and $asc <= -19776) return "B";
    if($asc >= -19775 and $asc <= -19219) return "C";
    if($asc >= -19218 and $asc <= -18711) return "D";
    if($asc >= -18710 and $asc <= -18527) return "E";
    if($asc >= -18526 and $asc <= -18240) return "F";
    if($asc >= -18239 and $asc <= -17923) return "G";
    if($asc >= -17922 and $asc <= -17418) return "H";
    if($asc >= -17417 and $asc <= -16475) return "J";
    if($asc >= -16474 and $asc <= -16213) return "K";
    if($asc >= -16212 and $asc <= -15641) return "L";
    if($asc >= -15640 and $asc <= -15166) return "M";
    if($asc >= -15165 and $asc <= -14923) return "N";
    if($asc >= -14922 and $asc <= -14915) return "O";
    if($asc >= -14914 and $asc <= -14631) return "P";
    if($asc >= -14630 and $asc <= -14150) return "Q";
    if($asc >= -14149 and $asc <= -14091) return "R";
    if($asc >= -14090 and $asc <= -13319) return "S";
    if($asc >= -13318 and $asc <= -12839) return "T";
    if($asc >= -12838 and $asc <= -12557) return "W";
    if($asc >= -12556 and $asc <= -11848) return "X";
    if($asc >= -11847 and $asc <= -11056) return "Y";
    if($asc >= -11055 and $asc <= -10247) return "Z";
    return null;
}
